package com.imxinxin.razer.macro;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.UUID;

public class RazerMacroPUBG {


    public static void main(String[] args) throws IOException {
        buildMacro();
    }

    static String MACRO_NAME = "MacroPro";
    static double Y_PIXEL = 1440;
    static Double FACTOR = Y_PIXEL / 108;



    public static void buildMacro() throws IOException {
        Document document = DocumentHelper.createDocument();
        Element macro = document.addElement("Macro");
//        macro.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
//        macro.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");

        macro.addElement("Name").setText(MACRO_NAME);
        macro.addElement("Guid").setText(UUID.randomUUID().toString());

        Element events = macro.addElement("MacroEvents");

//        addButton(events, "0");


        addMovement(events);

//        addButton(events, "1");


        macro.addElement("IsFolder").setText("false");
        macro.addElement("FolderGuid").setText("00000000-0000-0000-0000-000000000000");

        OutputFormat outputFormat = new OutputFormat();
        outputFormat.setEncoding("UTF-8");
        outputFormat.setNewlines(true);
        outputFormat.setIndent(true);
//        outputFormat.setIndent("  ");


        XMLWriter xmlWriter = new XMLWriter(new Writer() {
            @Override
            public void write(char[] cbuf, int off, int len) throws IOException {
                System.out.print(new String(cbuf, off, len));
            }

            @Override
            public void flush() throws IOException {

            }

            @Override
            public void close() throws IOException {

            }
        }, outputFormat);
        xmlWriter.write(document);

        xmlWriter = new XMLWriter(new FileWriter("C:/Users/eisen/Desktop/" + MACRO_NAME + ".xml"), outputFormat);

        xmlWriter.write(document);
        xmlWriter.close();

//        String xml = document.asXML();
//        System.out.println(xml);
    }


    private static void addButton(Element events, String state) {

        Element event = events.addElement("MacroEvent");
        event.addElement("Type").setText("2");
        Element button = event.addElement("MouseEvent");
        button.addElement("MouseButton").setText("1");
        button.addElement("State").setText(state);

    }

    private static void addMovement(Element events) {

        Element macroEvent = events.addElement("MacroEvent");
        macroEvent.addElement("Type").setText("3");
        Element mouseMovement = macroEvent.addElement("MouseMovement");
        addMouseMovementEvent(mouseMovement);

    }


    public static void addMouseMovementEvent(Element mouseMovement) {

        for (int i = 1; i <= 2; i++) {

            Double f = i * FACTOR;
            Double y = f + i;

            Element mouseMovementEvent = mouseMovement.addElement("MouseMovementEvent");
            mouseMovementEvent.addElement("Type").setText("3");
            mouseMovementEvent.addElement("Y").setText(String.valueOf(y.intValue()));
            mouseMovementEvent.addElement("Delay").setText("20");
        }
    }


}
